@extends('admin::public.base')
<style>
.layui-form-item .layui-input-inline {
        width: auto;
    }
</style>
@section('content')
    <div class="layui-card">
            <div class="layui-header">
                <div class="layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-input-inline">
                        <input type="text" name="filter" value="" id="filter" autocomplete="off" placeholder="请输入配置名称" class="layui-input layui-search">
                    </div>
                    <div class="layui-input-inline">
                        <button type="submit" class="layui-btn layui-btn-search" onclick="reload();">查询</button>
                    </div>
                    <a class="layui-btn" href="{{ route('admin.config.create') }}">添 加</a>
                </div>
            </div>
            <table id="dataTable" class="layui-hide" lay-filter="dataTable"></table>
            <script type="text/html" id="options">
                <a class="normal" lay-event="edit">编辑</a>
                <a class="normal" lay-event="del">删除</a>
            </script>
    </div>
@endsection

@section('script')
    {{--@can('system.role')--}}
    <script>
        layui.use(['layer','table','form'],function () {
            var layer = layui.layer;
            var form = layui.form;
            var table = layui.table;
            var param = {!!$param!!};
            var page = param.page?param.page:1;
            var limit = param.limit?param.limit:10;
            //用户表格初始化
            var dataTable = table.render({
                elem: '#dataTable'
                ,url: "{{ route('admin.config.getList') }}" //数据接口
                ,skin:'line'
                ,cellMinWidth: 150
                ,text: { //自定义文本，此处用法--》当返回数据为空时的异常提示
                    none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
                }
                ,page: {
                    curr:page,
                    limit:limit
                } //开启分页
                ,cols: [[ //表头
                    {checkbox: true}
                    ,{field: 'name',align:'center',  title: '配置项'}
                    ,{field: 'title', align:'center', title: '名称'}
                    ,{field: 'type', align:'center', title: '类型'}
                    ,{field: 'sort', align:'center', title: '排序',edit:'text'}
                    ,{field: 'desc', align:'center', title: '描述'}
                    ,{field: 'create_time',align:'center',  title: '创建时间'}
                    ,{field: 'update_time',align:'center',  title: '更新时间'}
                    ,{title: '操作',align:'center', toolbar: '#options'}
                ]]
            });
            window.reload = function(){
                layer.msg('数据重载中，请耐心等待', {
                    icon: 16,shade: 0.01,time:0
                });
                dataTable.reload({
                    page: {
                        curr: 1 //重新从第 1 页开始
                    },
                    where: {
                        filter:$('#filter').val(),
                    },
                    done: function(res, curr, count) {
                        layer.closeAll();
                    }
                })
            }
            table.on('edit(dataTable)', function(obj){ //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
                var value = obj.value;
                if(isNaN(value)){
                    layer.msg('请输入数字');
                    return false;
                }
                $.post("{{ route('admin.config.editSort') }}",{
                    name:obj.data.name,
                    value:value
                },function (data) {
                    if (data.code == 1) {
                        layer.msg(data.msg, {
                            time: 2000
                        }, function () {
                            reload();
                        })
                    } else {
                        layer.msg(data.msg);
                    }
                });
            });
            //监听复选框
            table.on('checkbox(dataTable)', function(obj){
                if(obj.checked){
                    if(obj.type=="all"){
                        $('.layui-table').find('tbody tr').attr({"style":"background:#f5fbff"});
                    }else{
                        var index = obj.data.LAY_TABLE_INDEX;
                        $('.layui-table').find('tbody tr').eq(index).attr({"style":"background:#f5fbff"});
                    }
                }else{
                    if(obj.type=="all"){
                        $('.layui-table').find('tbody tr').attr({"style":"background:#ffffff"});
                    }else{
                        var index = obj.data.LAY_TABLE_INDEX;
                        $('.layui-table').find('tbody tr').eq(index).attr({"style":"background:#ffffff"});
                    }
                }
            });
            //监听工具条
            table.on('tool(dataTable)', function(obj){ //注：tool是工具条事件名，dataTable是table原始容器的属性 lay-filter="对应的值"
                var data = obj.data //获得当前行数据
                    ,layEvent = obj.event; //获得 lay-event 对应的值
                if(layEvent === 'del'){
                    layer.confirm('您确认要删除吗？',{ icon: 3, title: '温馨提示', skin: 'layui-skin'}, function(index){
                        $.post("{{ route('admin.config.destroy') }}",{_method:'delete',ids:[data.id]},function (data) {
                            if (data.code == 1) {
                                layer.msg(data.msg, {
                                    time: 2000
                                }, function () {
                                    reload();
                                })
                            } else {
                                layer.msg(data.msg);
                            }
                            layer.close(index);
                        });
                    });
                } else if(layEvent === 'edit'){
                    location.href = '/admin/config/'+data.id+'/edit';
                }
            });
        })
    </script>
    {{--@endcan--}}
@endsection
